package com.e.library.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 
 * @author lxy
 *
 * @param <A>
 */
public abstract class EFragment<A extends FragmentActivity> extends Fragment {

	protected String TAG;
	protected A mActivity;
	protected View mContentView;
    protected Unbinder unbinder;

    protected abstract int layout();
    protected abstract void onFragmentCreated(View contentView, Bundle bundle);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mContentView == null) {
            TAG = this.getClass().getSimpleName();
            mContentView = inflater.inflate(layout(), null);
            unbinder = ButterKnife.bind(this,mContentView);
            onFragmentCreated(mContentView,savedInstanceState);
        } else {

            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (null != parent) {
                parent.removeAllViewsInLayout();
            }
        }
        return mContentView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (A)activity;
    }

    public A activity(){
    	return mActivity;
    }

    public String TAG() {
        return TAG;
    }

}
